<?php

class TagModel extends Model {

    public function listTag($firstRow = 0, $listRows = 20) {
        $M = M("Tag");
        $list = $M->field("`id`,`name`,`cid`")->order("`id` DESC")->limit("$firstRow , $listRows")->select();

        $cidArr = M("Category")->field("`cid`,`name`")->select();
        foreach ($cidArr as $k => $v) {
            $cids[$v['cid']] = $v;
        }
        unset($cidArr);
        foreach ($list as $k => $v) {
            $list[$k]['cidName'] = $cids[$v['cid']]['name'];
        }
        return $list;
    }

    public function addTag() {
        $M = M("Tag");
        $data = $_POST['info'];

        $tag_id = $M->add($data);

        if ($tag_id) {

            return array('status' => 1, 'info' => "已经添加", 'url' => U('News/tag'));
        } else {
            return array('status' => 0, 'info' => "添加失败，请刷新页面尝试操作");
        }
    }

    public function edit() {
        $M = M("Tag");
        $data = $_POST['info'];

        if ($M->save($data)) {

            return array('status' => 1, 'info' => "已经更新", 'url' => U('News/tag'));
        } else {
            return array('status' => 0, 'info' => "更新失败，请刷新页面尝试操作",'data'=>$M->getLastSql());
        }
    }

}

?>
